package com.ruoyi.lxzx.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.lxzx.appDTO.lxlbDTO.SearchDTO;
import com.ruoyi.lxzx.appVO.lxxmVO.UserCircleDetailVO;
import com.ruoyi.lxzx.appVO.lxxmVO.UserCircleDiscussVO;
import com.ruoyi.lxzx.appVO.lxxmVO.UserCircleListVO;
import com.ruoyi.lxzx.mapper.UserCircleDiscussMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.UserCircleMapper;
import com.ruoyi.lxzx.domain.UserCircle;
import com.ruoyi.lxzx.service.IUserCircleService;

import static com.ruoyi.common.utils.PageUtils.startPage;

//import static com.ruoyi.lxzx.appVO.lxxmVO.UserCircleDiscussVO.buildTree;

/**
 * 老乡圈Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
public class UserCircleServiceImpl implements IUserCircleService {
    @Autowired
    private UserCircleMapper userCircleMapper;

    @Autowired
    private UserCircleDiscussMapper userCircleDiscussMapper;

    /**
     * 查询老乡圈
     *
     * @param id 老乡圈主键
     * @return 老乡圈
     */
    @Override
    public UserCircle selectUserCircleById(Long id) {
        return userCircleMapper.selectUserCircleById(id);
    }

    /**
     * 查询老乡圈列表
     *
     * @param userCircle 老乡圈
     * @return 老乡圈
     */
    @Override
    public List<UserCircle> selectUserCircleList(UserCircle userCircle) {
        return userCircleMapper.selectUserCircleList(userCircle);
    }

    /**
     * 新增老乡圈
     *
     * @param userCircle 老乡圈
     * @return 结果
     */
    @Override
    public int insertUserCircle(UserCircle userCircle) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        userCircle.setCreateBy(finalUserId + "");
        userCircle.setCreateTime(DateUtils.getNowDate());
        return userCircleMapper.insertUserCircle(userCircle);
    }

    /**
     * 修改老乡圈
     *
     * @param userCircle 老乡圈
     * @return 结果
     */
    @Override
    public int updateUserCircle(UserCircle userCircle) {
        userCircle.setUpdateTime(DateUtils.getNowDate());
        return userCircleMapper.updateUserCircle(userCircle);
    }

    /**
     * 批量删除老乡圈
     *
     * @param ids 需要删除的老乡圈主键
     * @return 结果
     */
    @Override
    public int deleteUserCircleByIds(Long[] ids) {
        return userCircleMapper.deleteUserCircleByIds(ids);
    }

    /**
     * 删除老乡圈信息
     *
     * @param id 老乡圈主键
     * @return 结果
     */
    @Override
    public int deleteUserCircleById(Long id) {
        return userCircleMapper.deleteUserCircleById(id);
    }

    @Override
    public void fbxm(UserCircle userCircle) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        userCircle.setUserId(finalUserId);
        this.insertUserCircle(userCircle);
    }

    @Override
    public PageInfo xmlb(SearchDTO param) {
        startPage();
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        List<UserCircleListVO> result = userCircleMapper.xmlb();
        PageInfo pageInfo = new PageInfo(result);
        List<UserCircleListVO> res = pageInfo.getList();
        Long finalUserId1 = finalUserId;
        res.forEach(r->{
            if(finalUserId1 == 0L){
                r.setIsLike("0");
            }else {
                String isLike = userCircleMapper.isLike(r.getId(),finalUserId1);
                r.setIsLike(StringUtils.isBlank(isLike)?"0":"1");
            }
        });
        return pageInfo;
    }

    @Override
    public UserCircleDetailVO xmxq(Long circleId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        UserCircleDetailVO result = userCircleMapper.xmxq(circleId);
        if(result == null){
            throw  new ServiceException("没有获取到对应信息");
        }
        if(finalUserId == 0L){
            result.setIsLike("0");
        }else {
            String isLike = userCircleMapper.isLike(result.getId(),finalUserId);
            result.setIsLike(StringUtils.isBlank(isLike)?"0":"1");
        }
        List<UserCircleDiscussVO> subList = userCircleDiscussMapper.findListByCircleId(circleId);
//        result.setDiscussList(buildTree(subList));
        result.setDiscussList(subList);
        return result;
    }

    @Override
    public PageInfo wdxmlb(SearchDTO param) {
        startPage();
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        List<UserCircleListVO> result = userCircleMapper.wdxmlb(finalUserId);
        PageInfo pageInfo = new PageInfo(result);
        List<UserCircleListVO> res = pageInfo.getList();
        Long finalUserId1 = finalUserId;
        res.forEach(r->{
            if(finalUserId1 == 0L){
                r.setIsLike("0");
            }else {
                String isLike = userCircleMapper.isLike(r.getId(),finalUserId1);
                r.setIsLike(StringUtils.isBlank(isLike)?"0":"1");
            }
        });
        return pageInfo;
    }

    @Override
    public void scxm(Long circleId) {
        Long finalUserId = 0L;
        Long[] adminArr = SecurityUtils.getAdminIds();
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        UserCircle uc = selectUserCircleById(circleId);
        Long finalUserId1 = finalUserId;
        Optional<Long> result = Arrays.stream(adminArr).filter(num -> num == finalUserId1).findFirst();

        if(uc == null){
            throw new ServiceException("没有对应的动态信息");
        } else if (!uc.getCreateBy().equals(finalUserId+"") && !result.isPresent()) {
            throw new ServiceException("对应的动态不是你所创建,不能删除");
        }else {
            userCircleMapper.deleteUserCircleById(circleId);
        }
    }
}
